<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="/wx/page/session.jsp"%>
<c:set var="userId" value="${param.userId}" />
<c:set var="deptId" value="${param.deptId}" />
<c:set var="deptCode" value="${param.deptCode}" />
<c:set var="deptParentId" value="${param.deptParentId}" />
<c:set var="truckId" value="${param.truckId}" />
<c:set var="truckPlateCode" value="${param.truckPlateCode}" />
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, width=device-width">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="black" name="apple-mobile-web-app-status-bar-style">
    <title>车辆管理</title>
    <link rel="stylesheet" href="${ctx}/wx/css/default.css"/>
    <link rel="stylesheet" href="${ctx}/wx/css/picker.css"/>
    <script type="text/javascript" src="${ctx}/wx/js/picker.min.js"></script>
    <script type="text/javascript" src="${ctx}/wx/js/picker.data.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/util.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/interface/UserService.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/interface/TruckService.js"></script>
    <script type="text/javascript" src="${ctx}/wx/js/alert.js"></script>
</head>
<body>
<form id="truckForm" name="truckForm" method="post">
    <fieldset class="contact-inner">
        <input type="hidden" id="deptId" name="deptId" value="${deptId}">
        <input type="hidden" id="deptParentId" name="deptParentId" value="${deptParentId}">
        <input type="hidden" id="truckId" name="truckId" value="${truckId}">
        <input type="hidden" id="userId" name="userId" value="">
        <input type="hidden" id="userType" name="userType" value="3">
        <input type="hidden" id="userState" name="userState" value="1">
        <p class="contact-input">
            <select id="truckPlateHeader" name="truckPlateHeader" style="width: 30px" onchange="getTruckPlate()">
                <option value="蒙">蒙</option>
                <option value="晋">晋</option>
                <option value="冀">冀</option>
                <option value="辽">辽</option>
                <option value="鲁">鲁</option>
                <option value="陕">陕</option>
                <option value="豫">豫</option>
                <option value="津">津</option>
                <option value="京">京</option>
                <option value="黑">黑</option>
                <option value="吉">吉</option>
            </select>
            <select id="truckPlateMiddle" name="truckPlateMiddle" style="width: 30px" onchange="getTruckPlate()">
                <option value="A">A</option>
                <option value="B">B</option>
                <option value="C">C</option>
                <option value="D">D</option>
                <option value="E">E</option>
                <option value="F">F</option>
                <option value="G">G</option>
                <option value="H">H</option>
                <option value="I">I</option>
                <option value="J">J</option>
                <option value="K">K</option>
                <option value="L">L</option>
                <option value="M">M</option>
                <option value="N">N</option>
                <option value="O">O</option>
                <option value="P">P</option>
                <option value="Q">Q</option>
                <option value="R">R</option>
                <option value="S">S</option>
                <option value="T">T</option>
            </select>
            <input type="search" id="truckPlateCode" name="truckPlateCode" minlength="5" maxlength="5" placeholder="必填 车牌号" value="${truckPlateCode}" style="width: 120px" onchange="getTruckPlate()" required>
        </p>
        <p class="contact-input">
            <input type="tel" id="truckMobile" name="truckMobile" minlength="11" maxlength="11" placeholder="必填 车辆联系电话" onchange="getTruckMobile()" required>
            <input type="hidden" id="userMobile" name="userMobile" minlength="11" maxlength="11" placeholder="必填 车主电话" required>
        </p>
        <p class="contact-input">
            <input type="radio" id="truckTypeHalf" name="truckType" style="width: 20px; height: 20px;" value="半挂车" checked>
            <label for="truckTypeHalf" style="width: 20px">半挂车</label>
            <input type="radio" id="truckTypeAll" name="truckType" style="width: 20px; height: 20px;" value="全挂车">
            <label for="truckTypeAll" style="width: 20px">全挂车</label>
        </p>
        <p class="contact-input">
            <input type="search" id="userName" name="userName" minlength="2" maxlength="10" placeholder="必填 车主姓名" required>
        </p>
        <p class="contact-input">
            <input type="radio" id="userSex1" name="userSex" style="width: 20px; height: 20px;" value="男" checked>
            <label for="truckTypeAll" style="width: 20px">男</label>
            <input type="radio" id="userSex0" name="userSex" style="width: 20px; height: 20px;" value="女">
            <label for="truckTypeHalf" style="width: 20px">女</label>
        </p>
        <p class="contact-input">
            <input type="tel" id="truckLicence" name="truckLicence" maxlength="18" placeholder="行驶证">
        </p>
        <p class="contact-input">
            <input type="tel" id="userNo" name="userNo" maxlength="18" placeholder="车主身份证号">
        </p>
        <p class="contact-input">
            <input type="tel" id="truckNative" name="truckNative" placeholder="籍贯" onfocus="picker2(this)" readonly>
        </p>
        <p class="contact-submit">
            <input type="button" value="保存车辆" onclick="insertTruck()">
        </p>
    </fieldset>
</form>
</body>
</html>
<script type="text/javascript">
    if("${truckId}" > 0) {
        var truck = new Truck();
        truck.truckId = "${truckId}";
        TruckService.selectTruckList(truck, "${deptParentId}", callbackSelectTruckList);
    }
    function picker2(input) {
        new Picker({
            //"title": '请选择',//标题(可选)
            "defaultValue": input.value,//默认值-多个以空格分开（可选）
            "type": 2,//需要联动级数[1、2、3]（可选）
            "data": cityData,//数据(必传)
            "keys": {
                "id": "Code",
                "value": "Name",
                "childData": "level"//最多3级联动
            },//数组内的键名称(必传，id、text、data)
            "callBack": function (value, id) {
                //回调函数（val为选择的值）
                input.value = value;
            }
        });
    }
    function getTruckPlate() {
        var truckPlateCode = dwr.util.getValue("truckPlateCode").toUpperCase();
        truckPlateCode = truckPlateCode.replace(/\s/gi, "");
        dwr.util.setValue("truckPlateCode", truckPlateCode);
        if(truckPlateCode.length == 5) {
            var truck = new Truck();
            truck.truckPlateHeader = dwr.util.getValue("truckPlateHeader");
            truck.truckPlateMiddle = dwr.util.getValue("truckPlateMiddle");
            truck.truckPlateCode = dwr.util.getValue("truckPlateCode");
            TruckService.selectTruckList(truck, null, callbackSelectTruckList);
        }
    }
    function getTruckMobile() {
        var truckMobile = dwr.util.getValue("truckMobile");
        if(truckMobile.length == 11) {
            var truck = new Truck();
            truck.truckMobile = truckMobile;
            TruckService.selectTruckList(truck, null, callbackSelectTruckList);
        }
    }
    function callbackSelectTruckList(list) {
        if(list.length > 0) {
            if("${truckId}" == 0) {
                alert("已存在该车辆信息");
            }
            var truck = list[0];
            dwr.util.setValues(truck);
            dwr.util.setValues(truck.user);
        }
    }
    function insertTruck() {
        dwr.util.setValue("userMobile", dwr.util.getValue("truckMobile"));
        if (validate($("truckForm"))) {
            var user = new User();
            user.userId = dwr.util.getValue("userId");
            user.userMobile = dwr.util.getValue("userMobile");
            user.userName = dwr.util.getValue("userName");
            user.userSex = dwr.util.getValue("userSex");
            user.userNo = dwr.util.getValue("userNo");
            user.userType = dwr.util.getValue("userType");
            user.userState = dwr.util.getValue("userState");

            var truck = new Truck();
            truck.truckId = dwr.util.getValue("truckId");
            truck.truckMobile = dwr.util.getValue("truckMobile");
            truck.truckPlateHeader = dwr.util.getValue("truckPlateHeader");
            truck.truckPlateMiddle = dwr.util.getValue("truckPlateMiddle");
            var truckPlateCode = dwr.util.getValue("truckPlateCode").toUpperCase();
            truckPlateCode = truckPlateCode.replace(/\s/gi, "");
            truck.truckPlateCode = truckPlateCode;
            truck.truckType = dwr.util.getValue("truckType");
            truck.truckNative = dwr.util.getValue("truckNative");
            truck.user = user;

            TruckService.insertTruck(truck, "${deptId}", "${deptParentId}", callback);
        }
    }
    function callback() {
        location = "${ctx}/wx/page/truck_list.jsp?userId=${userId}&deptId=${deptId}&deptCode=${deptCode}&deptParentId=${deptParentId}";
    }
</script>